<!DOCTYPE html><html><head><title>README</title><link href="http://www.jboss.org/jdf/stylesheets/documentation.css" rel="stylesheet"></link><link href="http://www.jboss.org/jdf/stylesheets/pygments.css" rel="stylesheet"></link></head><body>
<h1><a id="shoppingcart-ejb-31-stateful-session-bean-sfsb-example-" class="anchor" href="#shoppingcart-ejb-31-stateful-session-bean-sfsb-example-"><span class="anchor-icon"></span></a>shopping-cart: EJB 3.1 Stateful Session Bean (SFSB) Example </h1>

<p>Author: Serge Pagop<br/>
Level: Intermediate<br/>
Technologies: EJB<br/>
Summary: Demonstrates a stateful session bean<br/>
Target Product: EAP<br/>
Product Versions: EAP 6.1, EAP 6.2<br/>
Source: <a href="https://github.com/jboss-developer/jboss-eap-quickstarts/">https://github.com/jboss-developer/jboss-eap-quickstarts/</a>  </p>

<h2><a id="what-is-it" class="anchor" href="#what-is-it"><span class="anchor-icon"></span></a>What is it?</h2>

<p>In this example, you will learn how to deploy and run a simple Java EE 6 application named <code>shopping-cart</code> that uses a stateful session bean. The shopping-cart allows customers to buy, checkout and view their cart contents. </p>

<p>The shopping-cart application consists of the following:</p>

<ol>
<li><p>A server side component:</p>

<p>This standalone Java EE module is a JAR containing EJBs. It is responsible for managing the shopping cart.</p></li>
<li><p>A Java client:</p>

<p>This simple Java client is launched using a &ldquo;main&rdquo; method. The remote client looks up a reference to the server module&rsquo;s API, via JNDI. It then uses this API to perform the operations the customer requests.</p></li>
</ol>

<h2><a id="system-requirements" class="anchor" href="#system-requirements"><span class="anchor-icon"></span></a>System requirements</h2>

<p>The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 6.1 or later. </p>

<p>All you need to build this project is Java 6.0 (Java SDK 1.6) or later, Maven 3.0 or later.</p>

<h2><a id="configure-maven" class="anchor" href="#configure-maven"><span class="anchor-icon"></span></a>Configure Maven</h2>

<p>If you have not yet done so, you must <a href="../README.html#configure-maven">Configure Maven</a> before testing the quickstarts.</p>

<h2><a id="start-the-jboss-server" class="anchor" href="#start-the-jboss-server"><span class="anchor-icon"></span></a>Start the JBoss Server</h2>

<ol>
<li>Open a command line and navigate to the root of the JBoss server directory.</li>
<li><p>The following shows the command line to start the server:</p>
<div class="highlight"><pre><span class="n">For</span> <span class="n">Linux</span><span class="o">:</span>   <span class="n">JBOSS_HOME</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">standalone</span><span class="p">.</span><span class="n">sh</span>
<span class="n">For</span> <span class="n">Windows</span><span class="o">:</span> <span class="n">JBOSS_HOME</span><span class="err">\</span><span class="n">bin</span><span class="err">\</span><span class="n">standalone</span><span class="p">.</span><span class="n">bat</span>
</pre></div></li>
</ol>

<h2><a id="build-and-deploy-the-quickstart" class="anchor" href="#build-and-deploy-the-quickstart"><span class="anchor-icon"></span></a>Build and Deploy the Quickstart</h2>

<ol>
<li><p>Make sure you have started the JBoss server. See the instructions in the previous section.</p></li>
<li><p>Open a command line and navigate to the <code>shopping-cart</code> quickstart directory</p></li>
<li><p>To build both the server component and the remote client program, deploy the server module, change into the examples shopping-cart directory and type the following:</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">clean</span> <span class="n">install</span> <span class="n">jboss</span><span class="o">-</span><span class="n">as</span><span class="o">:</span><span class="n">deploy</span> 
</pre></div></li>
<li><p>This maven goal will deploy <code>server/target/jboss-shopping-cart-server.jar</code>. You can check the Application Server console to see information messages regarding the deployment.</p></li>
</ol>

<h2><a id="run-the-client-application" class="anchor" href="#run-the-client-application"><span class="anchor-icon"></span></a>Run the Client Application</h2>

<p>Now start a client that will access the beans you just deployed:</p>
<div class="highlight"><pre>    <span class="n">mvn</span> <span class="n">exec</span><span class="o">:</span><span class="n">java</span> <span class="o">-</span><span class="n">f</span> <span class="n">client</span><span class="o">/</span><span class="n">pom</span><span class="p">.</span><span class="n">xml</span> 
</pre></div>
<h2><a id="investigate-the-console-output" class="anchor" href="#investigate-the-console-output"><span class="anchor-icon"></span></a>Investigate the Console Output</h2>

<p>You should see the following: </p>

<ol>
<li>The client sends a remote method invocation to the stateful session bean to buy two &ldquo;JBoss Enterprise Application Platform 6&rdquo; subscriptions and one &ldquo;JBoss SOA Platform 6&rdquo; subscription.</li>
<li>The client sends a remote method invocation to get the contents of the cart and prints it to the console.</li>
<li>The client sends a remote method invocation to invoke checkout. Note the <code>checkout()</code> method in the server <code>ShoppingCartBean</code> has the <code>@Remove</code> annotation. This means the container will destroy shopping cart after the call and it will no longer be available. </li>
<li>The client calls <code>getCartContents()</code> to make sure the shopping cart was removed after checkout. This results in a <code>javax.ejb.NoSuchEJBException</code> trace in the server, proving the cart was removed.</li>
</ol>

<p>On the client console, you should see output similar to:</p>
<div class="highlight"><pre><span class="o">&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;</span>
<span class="n">Obtained</span> <span class="n">the</span> <span class="n">remote</span> <span class="n">interface</span> <span class="n">to</span> <span class="n">the</span> <span class="n">shopping</span> <span class="n">cart</span>
<span class="n">Buying</span> <span class="n">a</span> <span class="s">&quot;JBoss Enterprise Application Platform 6&quot;</span>
<span class="n">Buying</span> <span class="n">another</span> <span class="s">&quot;JBoss Enterprise Application Platform 6&quot;</span>
<span class="n">Buying</span> <span class="n">a</span> <span class="s">&quot;JBoss SOA Platform 6&quot;</span>

<span class="n">Print</span> <span class="n">cart</span><span class="o">:</span>
<span class="mi">1</span>     <span class="n">JBoss</span> <span class="n">SOA</span> <span class="n">Platform</span> <span class="mi">6</span>
<span class="mi">2</span>     <span class="n">JBoss</span> <span class="n">Enterprise</span> <span class="n">Application</span> <span class="n">Platform</span> <span class="mi">6</span>

<span class="n">Checkout</span>
<span class="n">Cart</span> <span class="n">was</span> <span class="n">correctly</span> <span class="n">removed</span><span class="p">,</span> <span class="n">as</span> <span class="n">expected</span><span class="p">,</span> <span class="n">after</span> <span class="n">Checkout</span>
<span class="o">&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;</span>
</pre></div>
<p>On the server console, you should see output similar to (remember the server messages might change for different versions):</p>
<div class="highlight"><pre><span class="n">INFO</span>  <span class="p">[</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">ejb3</span><span class="p">.</span><span class="n">deployment</span><span class="p">.</span><span class="n">processors</span><span class="p">.</span><span class="n">EjbJndiBindingsDeploymentUnitProcessor</span><span class="p">]</span> <span class="p">(</span><span class="n">MSC</span> <span class="n">service</span> <span class="kr">thread</span> <span class="mi">1</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span> <span class="n">JNDI</span> <span class="n">bindings</span> <span class="k">for</span> <span class="n">session</span> <span class="n">bean</span> <span class="n">named</span> <span class="n">ShoppingCartBean</span> <span class="n">in</span> <span class="n">deployment</span> <span class="n">unit</span> <span class="n">deployment</span> <span class="s">&quot;jboss-shopping-cart-server.jar&quot;</span> <span class="n">are</span> <span class="n">as</span> <span class="n">follows</span><span class="o">:</span>

    <span class="nl">java:</span><span class="n">global</span><span class="o">/</span><span class="n">jboss</span><span class="o">-</span><span class="n">shopping</span><span class="o">-</span><span class="n">cart</span><span class="o">-</span><span class="n">server</span><span class="o">/</span><span class="n">ShoppingCartBean</span><span class="o">!</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">sfsb</span><span class="p">.</span><span class="n">ShoppingCart</span>
    <span class="nl">java:</span><span class="n">app</span><span class="o">/</span><span class="n">jboss</span><span class="o">-</span><span class="n">shopping</span><span class="o">-</span><span class="n">cart</span><span class="o">-</span><span class="n">server</span><span class="o">/</span><span class="n">ShoppingCartBean</span><span class="o">!</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">sfsb</span><span class="p">.</span><span class="n">ShoppingCart</span>
    <span class="nl">java:</span><span class="n">module</span><span class="o">/</span><span class="n">ShoppingCartBean</span><span class="o">!</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">sfsb</span><span class="p">.</span><span class="n">ShoppingCart</span>
    <span class="nl">java:</span><span class="n">global</span><span class="o">/</span><span class="n">jboss</span><span class="o">-</span><span class="n">shopping</span><span class="o">-</span><span class="n">cart</span><span class="o">-</span><span class="n">server</span><span class="o">/</span><span class="n">ShoppingCartBean</span>
    <span class="nl">java:</span><span class="n">app</span><span class="o">/</span><span class="n">jboss</span><span class="o">-</span><span class="n">shopping</span><span class="o">-</span><span class="n">cart</span><span class="o">-</span><span class="n">server</span><span class="o">/</span><span class="n">ShoppingCartBean</span>
    <span class="nl">java:</span><span class="n">module</span><span class="o">/</span><span class="n">ShoppingCartBean</span>

<span class="n">INFO</span>  <span class="p">[</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">server</span><span class="p">]</span> <span class="p">(</span><span class="n">management</span><span class="o">-</span><span class="n">handler</span><span class="o">-</span><span class="n">threads</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="n">JBAS018559</span><span class="o">:</span> <span class="n">Deployed</span> <span class="s">&quot;jboss-shopping-cart-server.jar&quot;</span>
<span class="n">INFO</span>  <span class="p">[</span><span class="n">stdout</span><span class="p">]</span> <span class="p">(</span><span class="n">pool</span><span class="o">-</span><span class="mi">9</span><span class="o">-</span><span class="kr">thread</span><span class="o">-</span><span class="mi">8</span><span class="p">)</span> <span class="n">implementing</span> <span class="n">checkout</span><span class="p">()</span> <span class="n">left</span> <span class="n">as</span> <span class="n">exercise</span> <span class="k">for</span> <span class="n">the</span> <span class="n">reader</span><span class="o">!</span>
</pre></div>
<p><em>Note</em>: You also see the following <code>EJB Invocation failed</code> and <code>NoSuchEJBException</code> messages in the server log. This is the expected result because method is annotated with <code>@Remove</code>. This means the next invocation after the shopping cart checkout fails because the container has destroyed the instance and it is no longer available.</p>
<div class="highlight"><pre><span class="n">ERROR</span> <span class="p">[</span><span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">ejb3</span><span class="p">.</span><span class="n">invocation</span><span class="p">]</span> <span class="p">(</span><span class="n">EJB</span> <span class="k">default</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span> <span class="n">JBAS014134</span><span class="o">:</span> <span class="n">EJB</span> <span class="n">Invocation</span> <span class="n">failed</span> <span class="n">on</span> <span class="n">component</span> <span class="n">ShoppingCartBean</span> <span class="k">for</span> <span class="n">method</span> <span class="n">public</span> <span class="n">abstract</span> <span class="n">java</span><span class="p">.</span><span class="n">util</span><span class="p">.</span><span class="n">HashMap</span> <span class="n">org</span><span class="p">.</span><span class="n">jboss</span><span class="p">.</span><span class="n">as</span><span class="p">.</span><span class="n">quickstarts</span><span class="p">.</span><span class="n">sfsb</span><span class="p">.</span><span class="n">ShoppingCart</span><span class="p">.</span><span class="n">getCartContents</span><span class="p">()</span><span class="o">:</span> <span class="n">javax</span><span class="p">.</span><span class="n">ejb</span><span class="p">.</span><span class="n">NoSuchEJBException</span><span class="o">:</span> <span class="n">JBAS014300</span><span class="o">:</span> <span class="n">Could</span> <span class="n">not</span> <span class="n">find</span> <span class="n">EJB</span> <span class="n">with</span> <span class="n">id</span> <span class="p">{...]}</span>
</pre></div>
<h2><a id="undeploy-the-archive" class="anchor" href="#undeploy-the-archive"><span class="anchor-icon"></span></a>Undeploy the Archive</h2>

<ol>
<li>Make sure you have started the JBoss Server as described above.</li>
<li>Open a command line and navigate to the root directory of this quickstart.</li>
<li><p>When you are finished testing, type this command to undeploy the archive:</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">jboss</span><span class="o">-</span><span class="n">as</span><span class="o">:</span><span class="n">undeploy</span>
</pre></div></li>
</ol>

<h2><a id="run-the-quickstart-in-jboss-developer-studio-or-eclipse" class="anchor" href="#run-the-quickstart-in-jboss-developer-studio-or-eclipse"><span class="anchor-icon"></span></a>Run the Quickstart in JBoss Developer Studio or Eclipse</h2>

<p>You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see <a href="../README.html#use-jboss-developer-studio-or-eclipse-to-run-the-quickstarts">Use JBoss Developer Studio or Eclipse to Run the Quickstarts</a> </p>

<h2><a id="debug-the-application" class="anchor" href="#debug-the-application"><span class="anchor-icon"></span></a>Debug the Application</h2>

<p>If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.</p>
<div class="highlight"><pre>    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">sources</span>
    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">resolve</span> <span class="o">-</span><span class="n">Dclassifier</span><span class="o">=</span><span class="n">javadoc</span>
</pre></div></body></html>
